#include "DataInput.h"
#include "lang/Exception.h"
#include "lang/String.h"
#include "ByteConvert.h"
using namespace yzrilyzr_lang;
namespace yzrilyzr_io{
	void DataInput::inputOrder(ByteOrder order){
		bigEndian=order == ByteOrder::BIG_ENDIAN;
	}
	void DataInput::readFully(yzrilyzr_array::ByteArray & b){
		readFully(b, 0, b.length);
	}
	bool DataInput::readBoolean(){
		readFully(readBuffer, 0, 1);
		return readBuffer[0] != 0;
	}
	int8_t DataInput::readByte(){
		readFully(readBuffer, 0, 1);
		return readBuffer[0];
	}
	uint8_t DataInput::readUnsignedByte(){
		readFully(readBuffer, 0, 1);
		return static_cast<uint8_t>(readBuffer[0] & 0xff);
	}
	int16_t DataInput::readShort(){
		readFully(readBuffer, 0, 2);
		return ByteConvert::getShort(readBuffer, 0, bigEndian);
	}
	uint16_t DataInput::readUnsignedShort(){
		readFully(readBuffer, 0, 2);
		return ByteConvert::getUnsignedShort(readBuffer, 0, bigEndian);
	}
	int32_t DataInput::readInt(){
		readFully(readBuffer, 0, 4);
		return ByteConvert::getInt(readBuffer, 0, bigEndian);
	}
	uint32_t DataInput::readUnsignedInt(){
		readFully(readBuffer, 0, 4);
		return ByteConvert::getUnsignedInt(readBuffer, 0, bigEndian);
	}
	int64_t DataInput::readLong(){
		readFully(readBuffer, 0, 8);
		return ByteConvert::getLong(readBuffer, 0, bigEndian);
	}
	uint64_t DataInput::readUnsignedLong(){
		readFully(readBuffer, 0, 8);
		return ByteConvert::getUnsignedLong(readBuffer, 0, bigEndian);
	}
	float DataInput::readFloat(){
		readFully(readBuffer, 0, 4);
		return ByteConvert::getFloat(readBuffer, 0, bigEndian);
	}
	double DataInput::readDouble(){
		readFully(readBuffer, 0, 8);
		return ByteConvert::getDouble(readBuffer, 0, bigEndian);
	}
	String DataInput::readUTF(){
		u_index len=readShort();
		yzrilyzr_array::ByteArray utf(len);
		readFully(utf, 0, len);
		return String(utf, UTF8);
	}
}